﻿#include "SampleProperty.h"
#include "DatabaseMgmt.h"

CSampleProperty::CSampleProperty()
{

}

CSampleProperty::~CSampleProperty()
{

}

int CSampleProperty::queryByReportUid(uint32_t report_id, SampleProperty& property)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT UID,REPORT_UID,PACK_DESCRIBE,PACK_IMAGE,EXTERIOR_DESCRIBE,EXTERIOR_IMAGE"
		" FROM TB_SAMPLE_PROPERTY WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		if (query.next())
		{
			property.uid = query.value("UID").toUInt();
			property.report_uid = query.value("REPORT_UID").toUInt();
			property.pack_describe = query.value("PACK_DESCRIBE").toString();
			property.pack_image = query.value("PACK_IMAGE").toString();
			property.exterior_describe = query.value("EXTERIOR_DESCRIBE").toString();
			property.exterior_image = query.value("EXTERIOR_IMAGE").toString();
		}
	}

	return ret;
}

int CSampleProperty::removeByReportUid(uint32_t report_id)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_SAMPLE_PROPERTY WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CSampleProperty::modifyByReportUid(SampleProperty property)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("UPDATE TB_SAMPLE_PROPERTY SET PACK_DESCRIBE='%1',PACK_IMAGE='%2',"
		" EXTERIOR_DESCRIBE='%3',EXTERIOR_IMAGE='%4'"
		" WHERE REPORT_UID=%5")
		.arg(property.pack_describe)
		.arg(property.pack_image)
		.arg(property.exterior_describe)
		.arg(property.exterior_image)
		.arg(property.report_uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CSampleProperty::insertRow(SampleProperty property)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("INSERT INTO TB_SAMPLE_PROPERTY (REPORT_UID,PACK_DESCRIBE,PACK_IMAGE,"
		" EXTERIOR_DESCRIBE,EXTERIOR_IMAGE)"
		" VALUES (%1,'%2','%3','%4','%5');")
		.arg(property.report_uid)
		.arg(property.pack_describe)
		.arg(property.pack_image)
		.arg(property.exterior_describe)
		.arg(property.exterior_image);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	return ret;
}
